package osb.map.map;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.Projection;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

/**
 * 地图测距类
 */

public class Map_Ceju extends ItemizedOverlay<OverlayItem>
{
	private List<GeoPoint> GeoPointList = new ArrayList<GeoPoint>(); // 所有的数据点
	private static Context mContext;
	static private GeoPoint StartPoint = null;  // 起始点 上一个加入的记录
	static private GeoPoint FinishPoint = null; // 最后一个加入的数据
	static boolean IsOnTap = false;  // 是否是点击事件
    static float Total_Distance=0;   //总长度
	static Map_Ceju overitem;
	public static void StartCeJu()   // 启用测距功能 .点击测距按钮是 响应这个函数
	{
		overitem=new Map_Ceju(null);
		Map.Public_Static_Map.mapView.getOverlays().add(overitem); // 添加图层到mapview
	}

	public static void CloseCeJu() // 关闭测距
	{
		Map.Public_Static_Map.mapView.invalidate();		
//		Map.Public_Static_Map.mapView.getOverlays().clear();
		Map.Public_Static_Map.mapView.getOverlays().remove(overitem);
		Map.Public_Static_Map.mapView.invalidate();
		ClearAllData();
	}

	public static double Compute_Distance(GeoPoint startPoint,GeoPoint finishPoint) // 计算距离
	{
		double lat_startPoint = startPoint.getLatitudeE6() / 1E6;
		double lon_startPoint = startPoint.getLongitudeE6() / 1E6;
		double lat_finishPoint = finishPoint.getLatitudeE6() / 1E6;
		double lon_finishPoint = finishPoint.getLongitudeE6() / 1E6;

		double r = 6378.137;
		lat_startPoint = Math.toRadians(lat_startPoint);
		lon_startPoint = Math.toRadians(lon_startPoint);
		lat_finishPoint = Math.toRadians(lat_finishPoint);
		lon_finishPoint = Math.toRadians(lon_finishPoint);
		double d1 = Math.abs(lat_startPoint - lat_finishPoint);
		double d2 = Math.abs(lon_startPoint - lon_finishPoint);
		double p = Math.pow(Math.sin(d1 / 2), 2) + Math.cos(lat_startPoint) * Math.cos(lat_finishPoint) * Math.pow(Math.sin(d2 / 2), 2);
		double dis = r * 2 * Math.asin(Math.sqrt(p));

//		int i = (int) dis;
		
		BigDecimal a = new BigDecimal(dis);
		BigDecimal result = a.setScale(3, RoundingMode.DOWN);    //截取小数点后3位
	
		float j=result.floatValue();  
		Total_Distance+=j;

		if(Total_Distance<1)    //1公里一下的长度
		{
			String distance = "" + Total_Distance*1000;//计算多次的总长
			Toast.makeText(mContext, distance+"米", Toast.LENGTH_SHORT).show();
		}
		else {
			String distance = "" + Total_Distance;
			Toast.makeText(mContext, distance+"公里", Toast.LENGTH_SHORT).show();
		}	
		return dis;
	}

	public static void ClearAllData()   //清空所有数据
	{
		StartPoint = null;
		FinishPoint = null;
		IsOnTap = false;
		Total_Distance=0;
	}
	// 功能函数
	public Map_Ceju(Drawable arg0)
	{
		super(arg0);
		mContext = Map.Public_Static_Map; // 对象赋值
		// 初始化时清空些数据。因为他们是static
		GeoPointList.clear();
		StartPoint = null;
		FinishPoint = null;
		IsOnTap = false;
		Total_Distance=0;
		//
		populate();
		// TODO Auto-generated constructor stub
	}

	public void updateOverlay()
	{
		populate();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow)
	{
		Projection projection = mapView.getProjection();// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
		    // 定义一个圆
			Paint fillPaint = new Paint();
			fillPaint.setColor(Color.rgb(26,179,31));
			fillPaint.setAntiAlias(true);
			fillPaint.setStyle(Style.FILL);

			// 定义一种线argb(255, 255, 181, 216));
			Paint paint = new Paint();
			paint.setColor(Color.rgb(26,179,31));
			paint.setDither(true);
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeJoin(Paint.Join.ROUND);
			paint.setStrokeCap(Paint.Cap.ROUND);
			paint.setStrokeWidth(4);

			Point startPoint = null; // 起始点
			int listsize = GeoPointList.size(); // 链表里坐标的数量
			for (int i = 0; i < listsize; i++)
			{
				Point finishPoint = projection.toPixels(GeoPointList.get(i),
						null); // 下一个点 如果是第一次也是第一个点
				if (i == 0)
				{
					canvas.drawCircle(finishPoint.x, finishPoint.y, 5.0f,
							fillPaint);
					startPoint = finishPoint; // 第二个开始其实点 等于上一个终点
				} else
				{
					Path path = new Path();
					path.moveTo(startPoint.x, startPoint.y);
					path.lineTo(finishPoint.x, finishPoint.y); // 连线
					canvas.drawPath(path, paint);
					startPoint = finishPoint; // 第二个开始其实点 等于上一个终点
					canvas.drawCircle(finishPoint.x, finishPoint.y, 5.0f,fillPaint); // 连线后再画一个点
				}
			IsOnTap = false;
		}
		
	}

	@Override
	protected OverlayItem createItem(int arg0)
	{
		return null;
	}

	@Override
	public int size()
	{
		return 0;
	}

	@Override
	protected boolean onTap(int i)
	{
		Map.Public_Static_Map.mapView.invalidate();
		return true;
	}

	@Override
	public boolean onTap(GeoPoint arg0, MapView arg1)
	{
//		float lat = arg0.getLatitudeE6();
//		float lon = arg0.getLongitudeE6();
		if (arg0 != null)   // 判断是移动 事件 还是点击事件
		{
			IsOnTap = true; // 是点击事件
			
			GeoPointList.add(arg0); // 链表添加这个坐标数据
			if (StartPoint != null)
			{
				if (FinishPoint != null)
				{
					StartPoint = FinishPoint;
					FinishPoint = arg0;                        // 最后一个加入的数据 (第二个以上的点)
					Compute_Distance(StartPoint, FinishPoint); // 进行经纬到米的转换处理
				} else
				{
					FinishPoint = arg0;                        // 最后一个加入的数据 (第二个点)
					Compute_Distance(StartPoint, FinishPoint); // 进行经纬到米的转换处理
				}

			} else
			{
				StartPoint = arg0;   // 只有一个点的时候
			}

		} else
		{
			Map.Public_Static_Map.mapView.invalidate();
		}
		return super.onTap(arg0, arg1);
	}

}